package com.news.dao.impl;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.news.dao.BaseDao;
import com.news.dao.ReviewDao;
import com.news.entity.Admin;
import com.news.entity.News;
import com.news.entity.Review;
import com.news.entity.User;
import com.news.dao.AdminDao;
import com.news.dao.UserDao;
import com.news.dao.NewsDao;

public class ReviewDaoImpl extends BaseDao implements ReviewDao {
	// 实例化用户，新闻，管理员三个Dao类
	AdminDao adminDao = new AdminDaoImpl();
	UserDao userDao = new UserDaoImpl();
	NewsDao newsDao = new NewsDaoImpl();

	// 重写查询全部方法
	@Override
	public List<Review> queryAll() {
		// TODO Auto-generated method stub
		// 创建List
		List<Review> reviewList = new ArrayList<Review>();
		ResultSet rs = null;
		try {
			// 创建查询语句
			String sql = "SELECT * FROM Review";
			// 获取返回数据集
			rs = super.queryResultSet(sql);
			while (rs.next()) {
				Review review = new Review();
				review.setReviewId(rs.getInt("reviewId"));
				review.setUserId(rs.getInt("userId"));
				review.setAdminId(rs.getInt("adminId"));
				review.setNewsId(rs.getInt("newsId"));
				review.setReviewDetails(rs.getString("reviewDetails"));
				review.setReviewDate(rs.getTimestamp("reviewDate"));
				reviewList.add(review);
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			// TODO: handle finally clause
			super.closeAll(super.conn, super.ps, rs);
		}
		// 返回reviewList
		return reviewList;
	}

	// 重写查询单个评论方法
	@Override
	public Review queryReview(int reviewId) {
		// TODO Auto-generated method stub
		Review review = new Review();
		ResultSet rs = null;
		try {
			// 创建查询语句
			String sql = "SELECT * FROM Review WHERE reviewId =" + reviewId + "";
			// 获取返回数据集
			rs = super.queryResultSet(sql);
			while (rs.next()) {
				review.setReviewId(rs.getInt("reviewId"));
				review.setUserId(rs.getInt("userId"));
				review.setAdminId(rs.getInt("adminId"));
				review.setNewsId(rs.getInt("newsId"));
				review.setReviewDetails(rs.getString("reviewDetails"));
				review.setReviewDate(rs.getTimestamp("reviewDate"));
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			// TODO: handle finally clause
			super.closeAll(super.conn, super.ps, rs);
		}
		return review;
	}

	// 重写删除单个评论方法
	@Override
	public int deleteReview(int reviewId) {
		// TODO Auto-generated method stub
		int num = 0;
		try {
			String sql = "DELETE FROM Review WHERE reviewId=" + reviewId;
			System.out.println(sql);
			// 调用basedao（父类）的执行语句
			num = super.executeUpdate(sql, null);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			// TODO: handle finally clause
			super.closeAll(super.conn, super.ps);
		}
		return num;
	}

	// 重写删除多个方法
	@Override
	public int deleteReview(int[] reviewId) {
		// TODO Auto-generated method stub
		int num = 0;
		try {
			// 将id取出,并用逗号隔开
			String idAll = "";
			for (int i = 0; i < reviewId.length; i++) {
				// 当运行到最后一个数时,不加逗号
				if (i == reviewId.length - 1) {
					idAll += reviewId[i];
				} else {
					idAll += reviewId[i] + ",";
				}
			}
			System.out.println(idAll);
			String sql = "DELETE FROM Review WHERE reviewId IN(" + idAll + ")";
			System.out.println(sql);
			// 调用basedao(父类)的执行语句
			num = super.executeUpdate(sql, null);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			// TODO: handle finally clause
			super.closeAll(super.conn, super.ps);
		}
		return num;
	}

	// 重写修改单条评论方法
	@Override
	public int updateReview(Review review) {
		// TODO Auto-generated method stub
		int num = 0;
		String sql;
		try {
			//判断评论是用户的还是管理员的
			if(review.getUserId()==0) {
				sql= "UPDATE Review SET " + "adminId=?," + "newsId=?," + "reviewDetails=?,"
						+ "reviewDate=?" + " WHERE reviewId=?";
				Object[] obj = {review.getAdminId(), review.getNewsId(), review.getReviewDetails(),
						review.getReviewDate(),review.getReviewId() };
				// 调用basedao（父类）的执行语句
				num = super.executeUpdate(sql, obj);
			}else {
				sql= "UPDATE Review SET " + "userId=?," + "newsId=?," + "reviewDetails=?,"
						+ "reviewDate=?" + " WHERE reviewId=?";
				Object[] obj = { review.getUserId(), review.getNewsId(), review.getReviewDetails(),
						review.getReviewDate(),review.getReviewId() };
				// 调用basedao（父类）的执行语句
				num = super.executeUpdate(sql, obj);
			}
			
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			// TODO: handle finally clause
			super.closeAll(super.conn, super.ps);
		}
		return num;
	}

	// 重写添加评论方法
	@Override
	public int insertReview(Review review) {
		// TODO Auto-generated method stub
		int num = 0;
		String sql;
		System.err.println("添加评论方法"+review.toString());
		try {
			//判断是不是管理员,如果等于零，不是管理员
			if(review.getAdminId()==0) {
				sql = "INSERT INTO Review(userId,newsId,reviewDetails,reviewDate) VALUES (?,?,?,?)";
				Object[] obj = { review.getUserId(),  review.getNewsId(), review.getReviewDetails(),
						review.getReviewDate() };
				num = super.executeUpdate(sql, obj);
			}else {
				sql = "INSERT INTO Review(adminId,newsId,reviewDetails,reviewDate) VALUES (?,?,?,?)";
				Object[] obj = { review.getAdminId(), review.getNewsId(), review.getReviewDetails(),
						review.getReviewDate() };
				num = super.executeUpdate(sql, obj);
			}
			// 调用basedao的执行语句
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			// TODO: handle finally clause
			super.closeAll(super.conn, super.ps);
		}
		return num;
	}

	// 重写like模糊查询方法
	@Override
	public List<Review> likeSelectListReview(Review review) {
		// TODO Auto-generated method stub
		List<Review> reviewList = new ArrayList<Review>();
		ResultSet rs = null;
		System.err.println(review.toString());
		try {
			String sql = "SELECT * FROM Review WHERE 1 = 1 ";
			// 动态sql语句
			if (review.getReviewId() != 0) {
				sql += " and reviewId like '%" + review.getReviewId() + "%' ";
			}
			if (review.getUserId() != 0) {
				sql += " and userId like '%" + review.getUserId() + "%' ";
			}
			if (review.getAdminId() != 0) {
				sql += " and adminId like '%" + review.getAdminId() + "%' ";
			}
			if (review.getNewsId() != 0) {
				sql += " and newsId like '%" + review.getNewsId() + "%' ";
			}
			if (review.getReviewDetails()!=null) {
				sql += " and reviewDetails like '%" + review.getReviewDetails() + "%' ";
			}
			// 时间类型判断
			if (review.getReviewDate() != null) {
				sql += " and reviewDate like '%" + review.getReviewDate() + "%' ";
			}
			System.out.println(sql);
			rs = super.queryResultSet(sql);
			while (rs.next()) {
				Review rsReview = new Review();
				rsReview.setReviewId(rs.getInt("reviewId"));
				rsReview.setUserId(rs.getInt("userId"));
				rsReview.setAdminId(rs.getInt("adminId"));
				rsReview.setNewsId(rs.getInt("newsId"));
				rsReview.setReviewDetails(rs.getString("reviewDetails"));
				rsReview.setReviewDate(rs.getTimestamp("reviewDate"));
				reviewList.add(rsReview);
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			// TODO: handle finally clause
			super.closeAll(super.conn, super.ps, rs);
		}
		return reviewList;
	}

	// 重写查询用户类方法
	@Override
	public User selectUser(int userId) {
		// TODO Auto-generated method stub
		return userDao.queryUser(userId);
	}

	// 重写查询管理类方法
	@Override
	public Admin selectAdmin(int adminId) {
		// TODO Auto-generated method stub
		return adminDao.queryAdmin(adminId);
	}

	// 查询查询新闻类方法
	@Override
	public News selectNews(int newsId) {
		// TODO Auto-generated method stub
		return newsDao.queryNews(newsId);
	}

	//查询随机评论方法
	@Override
	public List<Review> likeSelectListReviewNum(int num) {
		// TODO Auto-generated method stub
		List<Review> reviewList = new ArrayList<Review>();
		ResultSet rs = null;
		try {
			// 创建查询语句
			String sql = "SELECT * FROM Review ORDER BY RAND() LIMIT "+num;
			// 获取返回数据集
			rs = super.queryResultSet(sql);
			while (rs.next()) {
				Review review = new Review();
				review.setReviewId(rs.getInt("reviewId"));
				review.setUserId(rs.getInt("userId"));
				review.setAdminId(rs.getInt("adminId"));
				review.setNewsId(rs.getInt("newsId"));
				review.setReviewDetails(rs.getString("reviewDetails"));
				review.setReviewDate(rs.getTimestamp("reviewDate"));
				reviewList.add(review);
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			// TODO: handle finally clause
			super.closeAll(super.conn, super.ps, rs);
		}
		// 返回reviewList
		return reviewList;
	}

	//根据新闻查评论
	@Override
	public List<Review> queryReviewInNews(int newsId) {
		// TODO Auto-generated method stub
		List<Review> reviewList = new ArrayList<Review>();
		ResultSet rs = null;
		try {
			// 创建查询语句
			String sql = "SELECT * FROM Review WHERE newsId= "+newsId;
			// 获取返回数据集
			rs = super.queryResultSet(sql);
			while (rs.next()) {
				Review review = new Review();
				review.setReviewId(rs.getInt("reviewId"));
				review.setUserId(rs.getInt("userId"));
				review.setAdminId(rs.getInt("adminId"));
				review.setNewsId(rs.getInt("newsId"));
				review.setReviewDetails(rs.getString("reviewDetails"));
				review.setReviewDate(rs.getTimestamp("reviewDate"));
				reviewList.add(review);
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			// TODO: handle finally clause
			super.closeAll(super.conn, super.ps, rs);
		}
		// 返回reviewList
		return reviewList;
	}

}
